﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using fcwms.Common;

public partial class admin_ruku_ruku_edit : SessionManage
{
    private string action = ActionEnum.Add.ToString(); //操作类型
    private string decla_id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string _action = DTRequest.GetQueryString("action");
        if (!string.IsNullOrEmpty(_action) && _action == ActionEnum.Edit.ToString())
        {
            this.action = ActionEnum.Edit.ToString();//修改类型
            this.decla_id = Request.QueryString["decla_id"];
            //if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
            //{
            //    MessageBox.Jscript(this, "传输参数不正确！", "back", "Error");
            //    return;
            //}
            if (!new fcwms.BLL.dt_ruku().Exists(Request.QueryString["decla_id"]))
            {
                MessageBox.Jscript(this, "信息不存在或已被删除！", "back", "Error");
                return;
            }
        }
        if (!IsPostBack)
        {
            string user_id = Session["username"].ToString();
            txtTransactor.Text = new fcwms.BLL.dt_users().GetTitle(user_id);
            txtRukuTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            RukuWayBind();
            RukuStorageBind();
            RukuStorageBind2();
            ShowInfo3();
            if (action == ActionEnum.Edit.ToString())
            {
                ShowInfo(this.decla_id);
            }
            
        }
    }

    #region 入库方式绑定==========================
    private void RukuWayBind()
    {
        fcwms.BLL.dt_ruku_way bll = new fcwms.BLL.dt_ruku_way();
        DataTable dt = bll.GetList("").Tables[0];
        ddlRukuWay.Items.Clear();
        ddlRukuWay.Items.Add(new ListItem("请选择入库方式...", ""));
        foreach (DataRow dr in dt.Rows)
        {
            ddlRukuWay.Items.Add(new ListItem(dr["ruku_way"].ToString(), dr["ruku_way"].ToString()));//列表显示的字段名称,字段名称对应的值
        }
    }
    #endregion

    #region 入库仓库绑定==========================
    private void RukuStorageBind()
    {
        if (action == ActionEnum.Edit.ToString()) //修改
        {
            fcwms.BLL.dt_storage bll = new fcwms.BLL.dt_storage();
            DataTable dt = bll.GetList("").Tables[0];
            ddlRukuStorage.Items.Clear();
            ddlRukuStorage.Items.Add(new ListItem("请选择入库仓库...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddlRukuStorage.Items.Add(new ListItem(dr["storage_name"].ToString(), dr["storage_name"].ToString()));
            }
        }
        else
        {
            fcwms.BLL.dt_storage bll = new fcwms.BLL.dt_storage();
            DataTable dt = bll.GetList("storage_status=0").Tables[0];
            ddlRukuStorage.Items.Clear();
            ddlRukuStorage.Items.Add(new ListItem("请选择入库仓库...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddlRukuStorage.Items.Add(new ListItem(dr["storage_name"].ToString(), dr["storage_name"].ToString()));
            }
        }
    }
    #endregion

    #region 入库仓库绑定2=========================
    private void RukuStorageBind2()
    {
        fcwms.BLL.dt_storage bll = new fcwms.BLL.dt_storage();
        DataTable dt = bll.GetList("").Tables[0];
        selRukuStorage.Items.Clear();
        selRukuStorage.Items.Add(new ListItem("请选择仓库号...", "0"));
        foreach (DataRow dr in dt.Rows)
        {
            selRukuStorage.Items.Add(new ListItem(dr["storage_name"].ToString(), dr["storage_name"].ToString()));
        }
    }
    #endregion

    #region 赋值操作==============================
    public void ShowInfo(string _decla_id)
    {
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        //fcwms.Model.dt_goods modelt = bll.GetModel(_decla_id);
        fcwms.Model.dt_ruku model = bll.GetModel(_decla_id);
        txtDeclaID.Text = model.decla_id;
        txtDeclaID2.Text = model.decla_id;
        txtDeclaID.ReadOnly = true;
        txtDeclaID.Enabled = false;
        hidDeclaID.Value = model.decla_id;
        txtCustomsBroker.Text = model.customs_broker;
        txtUnstuffingGroup.Text = model.unstuffing_group;
        txtTallyMan.Text = model.tallyman;
        txtCustomer.Text = model.customer;
        txtCustomsOfficer.Text = model.customs_officer;
        txtTransactor.Text = model.transactor;
        ddlRukuStorage.Value = model.ruku_storage;
        ddlRukuStorage.Disabled = true;
        ddlRukuWay.Value = model.ruku_way;
        txtRukuTime.Text = string.Format("{0:yyyy-MM-dd HH:mm}", model.ruku_time);
        //txtRukuTime.Text = Convert.ToDateTime(model.ruku_time).ToString("yyyy-MM-dd HH:mm");

        rptLocation.DataSource = model.dt_locations;
        rptLocation.DataBind();
        //rptNavList.DataSource = model.dt_goodss;
        //rptNavList.DataBind();
    }
    #endregion

    #region 库位显示==============================
    protected void selRukuStorage_ServerChange(object sender, EventArgs e)
    {
        lblShelf.Text = this.selShelf.Value;
        fcwms.BLL.dt_location bll = new fcwms.BLL.dt_location();
        rptList3.DataSource = bll.GetLocation("storage_id=" + this.selRukuStorage.Value + "and shelf_id=" + this.selShelf.Value);
        rptList3.DataBind();
    }
    protected void selShelf_ServerChange(object sender, EventArgs e)
    {
        lblShelf.Text = this.selShelf.Value;
        fcwms.BLL.dt_location bll = new fcwms.BLL.dt_location();
        rptList3.DataSource = bll.GetLocation("storage_id=" + this.selRukuStorage.Value + "and shelf_id=" + this.selShelf.Value);
        rptList3.DataBind();
    }
    public void ShowInfo3()
    {
        lblShelf.Text = this.selShelf.Value;
        fcwms.BLL.dt_location bll = new fcwms.BLL.dt_location();
        rptList3.DataSource = bll.GetLocation("storage_id=" + this.selRukuStorage.Value + "and shelf_id=" + this.selShelf.Value);
        rptList3.DataBind();
    }
    #endregion

    #region 增加操作==============================
    private bool DoAdd()
    {
        bool result = true;
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        fcwms.Model.dt_ruku model = new fcwms.Model.dt_ruku();


        string goods_name = Request.Form["goods_name"];
        string goods_num = Request.Form["goods_num"];
        string goods_cube = Request.Form["goods_cube"];
        string remark = Request.Form["remark"];
        //string shell_name = Request.Form["shell_name"];
        if (!string.IsNullOrEmpty(goods_name) && !string.IsNullOrEmpty(goods_num) && !string.IsNullOrEmpty(goods_cube))
        {
            try
            {
                string[] nameArr = goods_name.Split(',');
                string[] numArr = goods_num.Split(',');
                string[] cubeArr = goods_cube.Split(',');
                string[] remarkArr = remark.Split(',');
                //string[] shellArr = shell_name.Split(',');
                List<fcwms.Model.dt_goods> ls = new List<fcwms.Model.dt_goods>();
                for (int i = 0; i < nameArr.Length; i++)
                {
                    ls.Add(new fcwms.Model.dt_goods { goods_name = nameArr[i].Trim(), num = decimal.Parse(numArr[i].Trim()), cube = decimal.Parse(cubeArr[i].Trim()), remark = remarkArr[i].Trim() });
                }
                model.dt_goodss = ls;
            }
            catch
            {
                result = false;
            }
        }

        string storage_id = Request.Form["storage_id"];
        string shelf_id = Request.Form["shelf_id"];
        string floor_id = Request.Form["floor_id"];
        string line_id = Request.Form["line_id"];
        if (!string.IsNullOrEmpty(storage_id) && !string.IsNullOrEmpty(shelf_id) && !string.IsNullOrEmpty(floor_id) && !string.IsNullOrEmpty(line_id))
        {
            try
            {
                string[] storageArr = storage_id.Split(',');
                string[] shelfArr = shelf_id.Split(',');
                string[] floorArr = floor_id.Split(',');
                string[] lineArr = line_id.Split(',');
                List<fcwms.Model.dt_location> ls = new List<fcwms.Model.dt_location>();
                for (int i = 0; i < storageArr.Length; i++)
                {
                    ls.Add(new fcwms.Model.dt_location { storage_id = storageArr[i].Trim(), shelf_id = shelfArr[i].Trim(), floor_id = floorArr[i].Trim(), line_id = lineArr[i].Trim() });
                }
                model.dt_locations = ls;
            }
            catch
            {
                result = false;
            }
        }
        model.ruku_time = Convert.ToDateTime(txtRukuTime.Text.Trim());
        model.ruku_way = ddlRukuWay.Value;
        model.ruku_storage = ddlRukuStorage.Value;
        model.decla_id = txtDeclaID.Text.Trim();
        model.customs_broker = txtCustomsBroker.Text.Trim();
        model.unstuffing_group = txtUnstuffingGroup.Text.Trim();
        model.tallyman = txtTallyMan.Text.Trim();
        model.customer = txtCustomer.Text.Trim();
        model.customs_officer = txtCustomsOfficer.Text.Trim();
        model.transactor = txtTransactor.Text.Trim();
        if (bll.Add(model) < 1)
        {
            result = false;
        }
        return result;
    }
    #endregion

    #region 修改操作==============================
    private bool DoEdit(string _decla_id)
    {
        bool result = true;
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        fcwms.Model.dt_ruku model = bll.GetModel(_decla_id);

        //物品信息
        string goods_id = Request.Form["goods_id"];
        string goods_name = Request.Form["goods_name"];
        string goods_num = Request.Form["goods_num"];
        string goods_cube = Request.Form["goods_cube"];
        string remark = Request.Form["remark"];
        //string shell_name = Request.Form["shell_name"];
        if (!string.IsNullOrEmpty(goods_id) && !string.IsNullOrEmpty(goods_name) && !string.IsNullOrEmpty(goods_num) && !string.IsNullOrEmpty(goods_cube))
        {
            try
            {
                string[] idArr = goods_id.Split(',');
                string[] nameArr = goods_name.Split(',');
                string[] numArr = goods_num.Split(',');
                string[] cubeArr = goods_cube.Split(',');
                string[] remarkArr = remark.Split(',');
                List<fcwms.Model.dt_goods> ls = new List<fcwms.Model.dt_goods>();
                for (int i = 0; i < nameArr.Length; i++)
                {
                    ls.Add(new fcwms.Model.dt_goods { id = int.Parse(idArr[i]), decla_id = model.decla_id, goods_name = nameArr[i].Trim(), num = decimal.Parse(numArr[i].Trim()), cube = decimal.Parse(cubeArr[i].Trim()), remark = remarkArr[i].Trim() });
                }
                model.dt_goodss = ls;
            }
            catch
            {
                result = false;
            }
        }

        //库位信息
        string location_id = Request.Form["location_id"];
        string storage_id = Request.Form["storage_id"];
        string shelf_id = Request.Form["shelf_id"];
        string floor_id = Request.Form["floor_id"];
        string line_id = Request.Form["line_id"];
        if (!string.IsNullOrEmpty(location_id) && !string.IsNullOrEmpty(storage_id) && !string.IsNullOrEmpty(shelf_id) && !string.IsNullOrEmpty(floor_id) && !string.IsNullOrEmpty(line_id))
        {
            try
            {
                string[] idArr = location_id.Split(',');
                string[] storageArr = storage_id.Split(',');
                string[] shelfArr = shelf_id.Split(',');
                string[] floorArr = floor_id.Split(',');
                string[] lineArr = line_id.Split(',');
                List<fcwms.Model.dt_location> ls = new List<fcwms.Model.dt_location>();
                for (int i = 0; i < storageArr.Length; i++)
                {
                    ls.Add(new fcwms.Model.dt_location { id = int.Parse(idArr[i]), decla_id = model.decla_id, storage_id = storageArr[i].Trim(), shelf_id = shelfArr[i].Trim(), floor_id = floorArr[i].Trim(), line_id = lineArr[i].Trim() });
                }
                model.dt_locations = ls;
            }
            catch
            {
                result = false;
            }
        }

        model.ruku_time = Convert.ToDateTime(txtRukuTime.Text.Trim());
        model.ruku_way = ddlRukuWay.Value;
        model.ruku_storage = ddlRukuStorage.Value;
        model.decla_id = txtDeclaID.Text.Trim();
        model.customs_broker = txtCustomsBroker.Text.Trim();
        model.unstuffing_group = txtUnstuffingGroup.Text.Trim();
        model.tallyman = txtTallyMan.Text.Trim();
        model.customer = txtCustomer.Text.Trim();
        model.customs_officer = txtCustomsOfficer.Text.Trim();
        model.transactor = txtTransactor.Text.Trim();
        if (!bll.Update(model))
        {
            result = false;
        }
        return result;
    }
    #endregion

    #region 状态修改==============================
    public bool DoEdit2(string _decla_id)
    {
        bool result = true;
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        fcwms.Model.dt_ruku model = bll.GetModel(_decla_id);
        model.decla_id = txtDeclaID.Text.Trim();
        model.ruku_storage = ddlRukuStorage.Value;

        if (!bll.UpdateStatus(model))
        {
            result = false;
        }
        return result;

    }
    #endregion

    #region 保存操作==============================
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (action == ActionEnum.Edit.ToString()) //修改
        {
            //ChkAdminLevel("sys_branch", ActionEnum.Edit.ToString()); //检查权限
            if (!DoEdit(this.decla_id))
            {
                MessageBox.Jscript(this, "保存过程中发生错误啦！", "", "Error");
                return;
            }
            MessageBox.Jscript(this, "修改入库信息成功啦", "ruku_list.aspx", "Success");
        }
        else //添加
        {
            //ChkAdminLevel("sys_branch", ActionEnum.Add.ToString()); //检查权限
            if (!DoAdd())
            {
                MessageBox.Jscript(this, "保存过程中发生错误啦！", "", "Error");
                return;
            }
            MessageBox.Jscript(this, "添加入库信息成功啦", "ruku_list.aspx", "Success");
        }
    }
    #endregion    

    //#region 完成入库操作==========================
    //protected void btnFinish_Click(object sender, EventArgs e)
    //{
    //    if (action == ActionEnum.Edit.ToString())
    //    {
    //        if (!DoEdit2(this.decla_id))
    //        {
    //            MessageBox.Jscript(this, "完成入库操作出错啦！", "", "Error");
    //            return;
    //        }
    //        MessageBox.Jscript(this, "完成入库成功啦！", "ruku_list.aspx", "Success");
    //    }
    //} 
    //#endregion

    //protected void btnBack_Click(object sender, EventArgs e)
    //{
    //    //MessageBox.Jscript(this, "修改入库信息成功啦", "ruku_list.aspx", "Success");
    //    Response.Redirect("ruku_list.aspx");
    //    Response.End();
    //    //Response.Write("<script>window.location.href='ruku_list.aspx';</script>");
    //    //Server.Transfer("ruku_list.aspx");
    //    //string str = "<script language=javascript>history.go(-2);</script>";
    //    //Response.Write(str); 
    //}


    //public string GetType(string line_id)
    //{


    //    for (int i = 0; i < rptList3.Items.Count; i++)
    //    {
    //        string floor_num2 = ((HiddenField)rptList3.Items[i].FindControl("hid00")).Value;
    //        return floor_num2;
    //    }





    //    string storage_id = "01";
    //    string shelf_id = "01";
    //    string floor_id =floor_num ;

    //    StringBuilder strSql = new StringBuilder();
    //    strSql.Append("select count(1) from dt_kuwei ");
    //    strSql.Append("where storage_id=@storage_id and shelf_id=@shelf_id and floor_id=@floor_id ");
    //    strSql.Append("and line_id=" + line_id);

    //    SqlParameter[] parms = new SqlParameter[] { 
    //        new SqlParameter("@storage_id",storage_id),
    //        new SqlParameter("@shelf_id",shelf_id),
    //        new SqlParameter("@floor_id",floor_id)};

    //    bool exist = DbHelperSQL.Exists(strSql.ToString(), parms);

    //    if (exist)
    //    {

    //        StringBuilder strSql2 = new StringBuilder();
    //        strSql2.Append("select top 1 type from dt_kuwei ");
    //        strSql2.Append("where storage_id=@storage_id and shelf_id=@shelf_id and floor_id=@floor_id ");
    //        strSql2.Append(" and line_id=" + line_id);

    //        SqlParameter[] parms2 = new SqlParameter[] { 
    //        new SqlParameter("@storage_id",storage_id),
    //        new SqlParameter("@shelf_id",shelf_id),
    //        new SqlParameter("@floor_id",floor_id)};
    //        string type = DbHelperSQL.GetSingle(strSql2.ToString(), parms2).ToString();
    //        if (string.IsNullOrEmpty(type))
    //        {
    //            return "";
    //        }
    //        return type;
    //    }
    //    return "";
    //}

    //public void rptlist3Bind()
    //{
    //    StringBuilder strSql = new StringBuilder();
    //    strSql.Append("select * from dt_shelf ");

    //    DataSet ds = DbHelperSQL.Query(strSql.ToString());

    //    rptList3.DataSource = ds.Tables[0].DefaultView;
    //    rptList3.DataBind();
    //}

    protected void Button1_Click(object sender, EventArgs e)
    {
       
        string hang = hidhang.Value;
        string lie = hidlie.Value;
        string title = hidKuwei.Value;
        MessageBox.Jscript(this, title + hang + lie, "", "Success");
    }

    private bool DoAdd2()
    {
        bool result = true;
        fcwms.BLL.dt_ruku bll = new fcwms.BLL.dt_ruku();
        fcwms.Model.dt_ruku model = new fcwms.Model.dt_ruku();



        if (bll.Add(model) < 1)
        {
            result = false;
        }
        return result;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

    }
}